<?php
namespace frontend\controllers;

use backend\models\UserPost;
use common\models\Category;
use common\models\Comment;
use common\models\Post;
use common\models\User;
use common\models\UserCategory;
use common\models\ZancaiPost;
use common\WuMing;
use Yii;
use yii\web\Controller;
use yii\web\Cookie;


/**
 * 接收用户AJAX过来的所有数据
 * Ajaxcontroller
 */
class AjaxController extends Controller
{
    /**收藏文章
     * @throws \Exception
     */
    public function actionCollect()
    {
        if (!Yii::$app->user->isGuest) {
            if (Yii::$app->request->isAjax) {
                $post_id = Yii::$app->request->post("id");
                $user_id = Yii::$app->user->id;
                $user_post = new UserPost();
                $result = $user_post->findOne(['user_id' => $user_id, 'post_id' => $post_id]);
                if ($result) {
                    $data['status'] = 1;
                    $data['msg'] = "您已取消收藏";
                    echo json_encode($data);
                    $result->delete();
                } else {
                    $data['status'] = 0;
                    $data['msg'] = "收藏成功";
                    echo json_encode($data);
                    $user_post->user_id = $user_id;
                    $user_post->post_id = $post_id;
                    $user_post->time = time();
                    $user_post->save();
                }
            }
        } else {
            $data['status'] = -1;
            $data['msg'] = "请先登录";
            echo json_encode($data);
        }

    }

    /**
     * 订阅板块
     * @throws \Exception
     */
    public function actionDingyue()
    {
        if (!Yii::$app->user->isGuest) {
            if (Yii::$app->request->isAjax) {
                $cate_id = Yii::$app->request->post("id");
                $user_id = Yii::$app->user->id;
                $user_cate = new UserCategory();
                //判断是否存在此分类
                $cate = Category::findOne($cate_id);
                if (!$cate) {
                    $data['status'] = 11;
                    $data['msg'] = "不存在此分类";
                    echo json_encode($data);
                    exit;
                }
                $result = $user_cate->findOne(['user_id' => $user_id, 'cate_id' => $cate_id]);
                if ($result) {
                    $data['status'] = 1;
                    $data['msg'] = "您已取消订阅";
                    $cate->dynum -=1;
                    $cate->save();
                    echo json_encode($data);
                    $result->delete();
                } else {
                    $data['status'] = 0;
                    $cate->dynum +=1;
                    $cate->save();
                    $data['msg'] = "订阅成功";
                    echo json_encode($data);
                    $user_cate->user_id = $user_id;
                    $user_cate->cate_id = $cate_id;
                    $user_cate->time = time();
                    $user_cate->save();
                }
            }
        } else {
            $data['status'] = -1;
            $data['msg'] = "请先登录";
            echo json_encode($data);
        }

    }

    /**
     * 允许用户未登录情况下点赞和踩
     */
    public function actionZancai()
    {

        if (Yii::$app->request->isAjax) {
            //获取到的参数
            $post_id = Yii::$app->request->post("id");
            $type = Yii::$app->request->post("type");
            $ip = Yii::$app->request->getUserIP();
            //查询当前IP是否已经对这篇文章点过赞或者踩
            $zancai = ZancaiPost::findOne([
                'ip_address' => $ip,
                'post_id' => $post_id,
            ]);

            //查询post
            $post = Post::findOne($post_id);
            $zancaipost = new ZancaiPost();

            if ($zancai) {
                if( $zancai->type == 0){

                    if($type == $zancai->type){
                        $data['status'] = -1;
                        $data['msg'] = "已点过赞";
                    }else{
                        $zancai->delete();
                        $post->zan -=1;
                        $post->cai +=1;
                        $post->save();

                        $zancaipost->ip_address = $ip;
                        $zancaipost->post_id = $post_id;
                        $zancaipost->type =1;
                        $zancaipost->save();

                        $data['status'] = 3;
                        $data['msg'] = "取消赞,点踩成功";
                    }

                }

                if( $zancai->type == 1){
                    if($type == $zancai->type){
                        $data['status'] = -1;
                        $data['msg'] = "已点过踩";
                    }else{
                        $zancai->delete();
                        $post->zan +=1;
                        $post->cai -=1;
                        $post->save();

                        $zancaipost->ip_address = $ip;
                        $zancaipost->post_id = $post_id;
                        $zancaipost->type =0;
                        $zancaipost->save();

                        $data['status'] = 4;
                        $data['msg'] = "取消踩,点赞成功";
                    }
                }
                echo json_encode($data);
                exit;
            }


            //如果查询不到
            if (!$post) {
                $data['status'] = -1;
                $data['msg'] = "Error!文章不存在";
                echo json_encode($data);

            } else {//如果查询到这个post
                if ($type == 0) {//赞
                    $data['status'] = 1;
                    $data['msg'] = "点赞成功";
                    echo json_encode($data);
                    $post->zan += 1;
                    $post->save();
                    $zancaipost->ip_address = $ip;
                    $zancaipost->post_id = $post_id;
                    $zancaipost->type =0;
                    $zancaipost->save();
                } elseif ($type == 1) { //踩
                    $data['status'] = 2;
                    $data['msg'] = "点踩成功";
                    echo json_encode($data);
                    $post->cai += 1;
                    $post->save();
                    $zancaipost->ip_address = $ip;
                    $zancaipost->post_id = $post_id;
                    $zancaipost->type = 1;
                    $zancaipost->save();
                } else { //如果恶意修改了type
                    $data['status'] = -1;
                    $data['msg'] = "Error!参数错误!";
                    echo json_encode($data);
                }
            }
        }

    }

    /**判断用户是否登录
     * @return bool
     */
    protected function isLogin()
    {
        if (Yii::$app->user->isGuest) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 用户在线时间
     */
    public function actionOnline()
    {
        //判断是否是ajax请求
        if(Yii::$app->request->isAjax){
            $session  =Yii::$app->session;
            //如果不存在这个session
            if(!$session['online']){
                $session['online'] =time();
            }
            //获取时差 时差如果大于设置的毫秒数  就操作数据库
            $diffTime = time() - $session['online'];
            if($diffTime > 300){
                //更新session
                $session['online'] =time();
                //更新数据库字段
                $user = User::findOne(Yii::$app->user->id);
                $user->online_time += 5;
                $user->save();
            }
        }
    }


    public function actionSignin()
    {
        if(Yii::$app->request->isAjax && !Yii::$app->user->isGuest){
            //获取用户信息
            $uid = Yii::$app->user->id;
            $user = User::findOne($uid);
            //获取当前时间和上次签到时间
            $current = date("Ymd");
            $last = $user->signin_time;
            if($current == $last){
                $data['status'] = 1;
                $data['msg'] = "今天已经签到";
                echo json_encode($data);
                return ;
            }
            $isAdjacentDay = WuMing::isStreakDays(getdate(strtotime($last)),getdate(strtotime($current)));
            //如果是相邻两天
            if($isAdjacentDay){
                $user->total_signin += 1;
                $user->current_signin +=1;
                if($user->current_signin > $user->max_signin){
                    $user->max_signin = $user->current_signin;
                }
                $user->signin_time = $current;
                $user->save();
                $data['status'] = 1;
                $data['msg'] = "签到成功,连续签到";
                echo json_encode($data);
            }else{
                $user->total_signin += 1;
                $user->current_signin = 1;
                if($user->current_signin > $user->max_signin){
                    $user->max_signin = $user->current_signin;
                }
                $user->signin_time = $current;
                $user->save();
                $data['status'] = 1;
                $data['msg'] = "签到成功,不连续,重新签到";
                echo json_encode($data);
            }
        }
    }

    public function actionGetTitle()
    {
        $url = Yii::$app->request->post("url");
        $result = WuMing::getRemoteTitle($url);
        if(!$result){
            $data['status'] = -1;
            $data['msg']="URL格式错误或不可访问";
            echo json_encode($data);
        }else{
            $data['status'] = 1;
            $data['msg']=$result;
            echo json_encode($data);
        }
    }


    public function actionRead()
    {
        $id = Yii::$app->request->post("id");
        $comment = Comment::findOne($id);
        $comment->msgstatus = 1;
        if($comment->save()){
            echo json_encode("success");
        }
    }
}
